package com.leoman.plz_p.weidget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.leoman.plz_p.R;
import com.leoman.plz_p.adapter.ExamListAdapter;
import com.leoman.plz_p.module.bean.QuestionStatusBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.com.corelib.utils.DisplayUtil;

/**
 * Created by guoh on 2018/7/7.
 */
public class ExamListDialog extends Dialog {

    @Bind(R.id.v_empty)
    View vEmpty;
    @Bind(R.id.tv_submit_result)
    TextView tvSubmitResult;
    @Bind(R.id.tv_right_count)
    TextView tvRightCount;
    @Bind(R.id.tv_error_count)
    TextView tvErrorCount;
    @Bind(R.id.tv_current_count)
    TextView tvCurrentCount;
    @Bind(R.id.tv_total_count)
    TextView tvTotalCount;
    @Bind(R.id.gv_exam_list)
    GridView gvExamList;
    private ExamListAdapter adapter;
    private GridView gridView;
    private boolean isNotNormalExam=true;

    public ExamListDialog(@NonNull Context context) {
        super(context);
    }

    public ExamListDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.DialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_exam_list);
        ButterKnife.bind(this);
        gridView = findViewById(R.id.gv_exam_list);
        findViewById(R.id.v_empty).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        Window win = getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.height = (int) (DisplayUtil.getScreenDispaly(getContext())[1]);
        lp.width = (int) (DisplayUtil.getScreenDispaly(getContext())[0]);
        win.setAttributes(lp);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }


    public void setQuestionList(List<QuestionStatusBean> questions, int currentPosition,boolean isNotNormalExam) {
        this.isNotNormalExam=isNotNormalExam;
        if (!isNotNormalExam){
            tvRightCount.setVisibility(View.GONE);
            tvErrorCount.setVisibility(View.GONE);
        }
        adapter = new ExamListAdapter(getContext());
        adapter.setIsNotNormalExam(isNotNormalExam);
        adapter.replaceAll(questions);
        adapter.setCuttentPosition(currentPosition);
        gridView.setAdapter(adapter);
        tvCurrentCount.setText(""+(currentPosition+1));
        tvTotalCount.setText("/"+questions.size()+"");
    }

    public void hideItem(){
        tvSubmitResult.setVisibility(View.GONE);
        tvRightCount.setVisibility(View.GONE);
        tvErrorCount.setVisibility(View.GONE);
    }

    public void setCount(int rightCount,int errorCount){
        tvRightCount.setText(rightCount+"");
        tvErrorCount.setText(errorCount+"");
    }

    public void setIsFormMistake(boolean f,View.OnClickListener ls){
        if (f)tvSubmitResult.setText("删除");
        tvSubmitResult.setOnClickListener(ls);
    }

    public void setSubmitClickListener(View.OnClickListener ls){
        tvSubmitResult.setOnClickListener(ls);
    }

    public void setOnItemClick(AdapterView.OnItemClickListener onItemClick){
        gridView.setOnItemClickListener(onItemClick);
    }

}
